﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Audio;
//using Microsoft.Xna.Framework.Content;
//using Microsoft.Xna.Framework.GamerServices;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Input;
//using Microsoft.Xna.Framework.Media;
//using Microsoft.Xna.Framework.Storage;
//using System.IO;
//using System.Xml.Serialization;
//using System.Diagnostics;

//public struct BillboardParticleElement
//{
//    Vector3 position;
//    Vector2 textureCoordinate;
//    Color color;
//    Vector4 data;

//    public Vector3 Position
//    {
//        get { return position; }
//        set { position = value; }
//    }
//    public Vector2 TextureCoordinate
//    {
//        get { return textureCoordinate; }
//        set { textureCoordinate = value; }
//    }
//    public Vector4 Data
//    {
//        get { return data; }
//        set { data = value; }
//    }
//    public Color Color
//    {
//        get { return color; }
//        set { color = value; }
//    }

//    public float SpriteSize
//    {
//        get { return data.X; }
//        set { data.X = value; }
//    }
//    public float AlphaValue
//    {
//        get { return data.Y; }
//        set { data.Y = value; }
//    }

//    public BillboardParticleElement(Vector3 Position, Vector2 TextureCoordinate, Vector2 XYScale, Color Color)
//    {
//        position = Position;
//        textureCoordinate = TextureCoordinate;
//        data = Vector4.One;
//        color = Color;
//    }
//    static public VertexElement[] VertexElements = new VertexElement[]
//    {
//        new VertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
//        new VertexElement(4*3,VertexElementFormat.Vector2,VertexElementUsage.TextureCoordinate,0),                
//        new VertexElement(4*5,VertexElementFormat.Color ,VertexElementUsage.Color,0),
//        new VertexElement(4*6,VertexElementFormat.Vector4,VertexElementUsage.Position,1),
//    };

//    public static int SizeInBytes = (3 + 2 + 1 + 4) * 4;
//}

//public class ParticleEmitter
//{
//    private DynamicVertexBuffer vb;
//    private DynamicIndexBuffer ib;
//    VertexDeclaration m_vDec;

//    public BillboardParticleElement[] particleArray;

//    public Vector3 myPosition;
//    public Vector3 myScale;
//    public Quaternion myRotation;

//    public Color particleColor;

//    public int partCount;

//    Effect shader;

//    Game game;

//    int nextParticle = 0;
//    Movable targetPos;
//    Vector3 myLastpos;

//    public ParticleEmitter(Game game, int particleCount, Movable target)
//    {
//        this.game = game;
//        myPosition = Vector3.Zero;
//        myScale = Vector3.One;
//        myRotation = new Quaternion(0, 0, 0, 1);

//        partCount = particleCount;
//        targetPos = target;

//        particleColor = Color.White;
//    }

//    public void LoadContent()
//    {
//        m_vDec = new VertexDeclaration(BillboardParticleElement.VertexElements);

//        shader = game.Content.Load<Effect>("Shaders/BillboardShader");
//        shader.Parameters["particleTexture"].SetValue(game.Content.Load<Texture2D>("Textures/Particle"));


//        myLastpos = targetPos.Position;

//        LoadParticles();
//    }

//    private void LoadParticles()
//    {
//        particleArray = new BillboardParticleElement[partCount * 4];

//        for (int p = 0; p < partCount; p += 4)
//        {
//            for (int thisP = 0; thisP < 4; thisP++)
//            {
//                int currentParticle = p + thisP;

//                particleArray[currentParticle] = new BillboardParticleElement();
//                particleArray[currentParticle].Position = myPosition;
//                particleArray[currentParticle].Color = particleColor;
//                particleArray[currentParticle].Data = new Vector4(1f, 1f, 0, 0);
//                switch (thisP)
//                {
//                    case 0:
//                        particleArray[currentParticle].TextureCoordinate = Vector2.Zero;
//                        break;
//                    case 1:
//                        particleArray[currentParticle].TextureCoordinate = new Vector2(1, 0);
//                        break;
//                    case 2:
//                        particleArray[currentParticle].TextureCoordinate = new Vector2(0, 1);
//                        break;
//                    case 3:
//                        particleArray[currentParticle].TextureCoordinate = Vector2.One;
//                        break;
//                }
//            }
//        }

//        short[] indices = new short[6 * partCount];

//        for (int part = 0; part < partCount; part++)
//        {
//            int off = part * 6;
//            int offVal = part * 4;

//            indices[off + 0] = (short)(0 + offVal);
//            indices[off + 1] = (short)(1 + offVal);
//            indices[off + 2] = (short)(2 + offVal);

//            indices[off + 3] = (short)(1 + offVal);
//            indices[off + 4] = (short)(3 + offVal);
//            indices[off + 5] = (short)(2 + offVal);
//        }

//        ib = new DynamicIndexBuffer(game.GraphicsDevice, typeof(short), 6 * partCount, BufferUsage.WriteOnly);
//        ib.SetData(indices);
//    }

//    public void Update(GameTime gameTime)
//    {
//        for (int p = 0; p < particleArray.Length; p += 4)
//        {
//            for (int thisP = 0; thisP < 4; thisP++)
//            {
//                if (p == nextParticle && myLastpos != myPosition)
//                {
//                    particleArray[p + thisP].Position = myLastpos;
//                    particleArray[p + thisP].Color = particleColor;
//                }

//                particleArray[p + thisP].SpriteSize = (Vector3.Distance(particleArray[p + thisP].Position, targetPos.Position) / 5);
//            }
//        }
//        nextParticle++;

//        if (nextParticle >= particleArray.Length / 4)
//            nextParticle = 0;

//        vb = new DynamicVertexBuffer(game.GraphicsDevice, typeof(BillboardParticleElement), 4 * partCount, BufferUsage.WriteOnly);
//        vb.SetData(particleArray);

//        myLastpos = targetPos.Position;
//    }

//    public void Draw(GameTime gameTime, Camera cam)
//    {
        
//        game.GraphicsDevice.SetVertexBuffer(vb);
//        game.GraphicsDevice.Indices =ib;

//        RasterizerState prevRasterizerState = game.GraphicsDevice.RasterizerState;
//        BlendState prevBlendState = game.GraphicsDevice.BlendState;

//        game.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
//        game.GraphicsDevice.RasterizerState = RasterizerState.CullNone;

//        Matrix World = Matrix.CreateScale(myScale) * Matrix.CreateFromQuaternion(myRotation) * Matrix.CreateTranslation(myPosition);
//        shader.Parameters["world"].SetValue(World);
//        Matrix vp = cam.ViewMatrix * cam.ProjectionMatrix;
//        shader.Parameters["vp"].SetValue(vp);


//        foreach (EffectPass pass in shader.CurrentTechnique.Passes)
//        {
//            pass.Apply();
//            game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4 * partCount, 0, partCount * 2);

//        }



//        game.GraphicsDevice.SetVertexBuffer(null);
//        game.GraphicsDevice.Indices = null;

//        game.GraphicsDevice.BlendState = prevBlendState;
//        game.GraphicsDevice.RasterizerState = prevRasterizerState;

//    }
//}